package com.uinnova.beetlstu.service.impl;

import com.uinnova.beetlstu.dao.BlogDao;
import com.uinnova.beetlstu.dao.MessageDao;
import com.uinnova.beetlstu.dao.UserDao;
import com.uinnova.beetlstu.model.Blog;
import com.uinnova.beetlstu.model.Message;
import com.uinnova.beetlstu.model.User;
import com.uinnova.beetlstu.service.BlogService;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
/*
*@author 王刚
*@date 2019-01-19 13:37
*/

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private BlogDao blogDao;

    @Autowired
    private MessageDao messageDao;
    /*
    根据用户名密码查询用户
     */
    @Override
    public User login(String userName, String password) {
        return userDao.createLambdaQuery()
                .andEq(User::getUserName,userName)
                .andEq(User::getPassword,password)
                .andEq(User::getDeleteFlag,false)
                .single();
//        single根据主键查询，查询不到返回null

    }

    /*
    *根据类别查询分页好的博客信息
    */
    @Override
    public PageQuery<Blog> pageBlog(long pageNumber, long pageSize, String keyword, String category) {
        LambdaQuery<Blog> query = blogDao.createLambdaQuery()
                .andEq(Blog::getDeleteFlag, false);
        if (!StringUtils.isEmpty(keyword)) {
            /*
             *trim（）移出字符串两侧空白
             */
            query.andLike(Blog::getTitle, "%" + keyword.trim() + "%");
        }
        if (!StringUtils.isEmpty(category)) {
            query.andEq(Blog::getCategory, category);
        }
        if (pageNumber > 0 && pageSize > 0) {
            return query.desc(Blog::getCreateTime).page(pageNumber, pageSize);
        }
        return null;
    }

    /*
    *根据id查询博客
    */
    @Override
    public Blog queryBlogById(Long blogId) {
        if(blogId == null){
            return null;
        }
        return blogDao.createLambdaQuery()
                .andEq(Blog::getId,blogId)
                .andEq(Blog::getDeleteFlag,false)
                .single();
    }

    /*
    *根据id查询博客的评论信息
    */
    @Override
    public PageQuery<Message> pageMsg(Long blogId, Integer pageNumber, Integer pageSize) {
        return messageDao.createLambdaQuery()
                .andEq(Message::getBlogId,blogId)
                .andEq(Message::getDeleteFlag,false)
                .desc(Message::getCreateTime)
                .page(pageNumber,pageSize);
    }

    /*
    *id查询博客的评论信息
    */

}
